//
//  UIImage + Ex.swift
//  照片选择
//
//  Created by 曹晓龙 on 15/10/26.
//  Copyright © 2015年 曹晓龙. All rights reserved.
//

import UIKit

// MARK: - UIImage 扩展
extension UIImage {
    
    ///  获取一个缩放过的图片
    ///
    ///  - returns: 如果图片小于指定宽度,直接返回原图,否则返回缩放后的图片
    func scaleImage() -> UIImage{
        
        let width:CGFloat = 600
        
        if size.width < width {
            
            return self
        }
        
        let height = size.height * width / size.width
        
        let rect = CGRect(x: 0, y: 0, width: width, height: height)
        
        UIGraphicsBeginImageContext(rect.size)
        
        self.drawInRect(rect)
        
        let image = UIGraphicsGetImageFromCurrentImageContext()
        
        UIGraphicsEndImageContext()
        
        return image
    }
}
